package com.windfone.web.action;

import java.util.List;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.model.Askreponse;
import com.windfone.model.Color;
import com.windfone.model.DetailMsg;
import com.windfone.model.PackageCompose;
import com.windfone.model.Product;
import com.windfone.service.AskReponseService;
import com.windfone.service.CommonService;
import com.windfone.service.ProductService;

public class DetailMobileAction extends ActionSupport {

	/**	serialVersionUID */
	private static final long serialVersionUID = 1L;
	private ProductService productService;
	private CommonService commonService;
	private List<Color> colors;
	private Product mobile;
	private int productId = -1;
	private List<PackageCompose> packages;
	private DetailMsg detailMsg;
	private List<Askreponse>  askReponses;
	private AskReponseService askReponseService;
 
 
	public AskReponseService getAskReponseService() {
		return askReponseService;
	}
	public void setAskReponseService(AskReponseService askReponseService) {
		this.askReponseService = askReponseService;
	}
	public List<Askreponse> getAskReponses() {
		return askReponses;
	}
	public void setAskReponses(List<Askreponse> askReponses) {
		this.askReponses = askReponses;
	}
	public DetailMsg getDetailMsg() {
		return detailMsg;
	}
	public void setDetailMsg(DetailMsg detailMsg) {
		this.detailMsg = detailMsg;
	}
	public List<PackageCompose> getPackages() {
		return packages;
	}
	public void setPackages(List<PackageCompose> packages) {
		this.packages = packages;
	}
	public int getProductId() {
		return productId;
	}
	public void setProductId(int productId) {
		this.productId = productId;
	}
	public List<Color> getColors() {
		return colors;
	}
	public void setColors(List<Color> colors) {
		this.colors = colors;
	}
	public void setCommonService(CommonService commonService) {
		this.commonService = commonService;
	}
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	@Override
	public String execute() throws Exception {
		if ( productId !=-1 ){
			mobile = productService.getProductById(productId).get(0);
			colors = commonService.getColorByProductId(productId);
			packages = commonService.getPackageByProductId(productId);
			detailMsg =commonService.getDetailMsgByProductId(productId);
			 	
			ServletContext application = ServletActionContext.getServletContext();  
	        WebApplicationContext wac = WebApplicationContextUtils       .getWebApplicationContext(application);
	        if ( askReponseService == null ) 
	        	askReponseService  = (AskReponseService )wac.getBean("askReponseService");
			askReponses = askReponseService.getAskReponseByProductAndType(productId, 1);
		}
		productId = -1;
		return SUCCESS;
	}
	public Product getMobile() {
		return mobile;
	}
	public void setMobile(Product mobile) {
		this.mobile = mobile;
	}

}
